//
//  LocalData.m
//  Coco_Music
//
//  Created by 强哥 on 15/7/3.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "LocalData.h"

@implementation LocalData



+(instancetype)shareLocalData
{
    static LocalData *data = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[LocalData alloc] init];
    });
    return data;
}
//scroll本地套装
-(void)openLocalData
{
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    file = [file stringByAppendingPathComponent:@"LocalData.sqlite"];
    NSLog(@"%@",file);
    self.Queue = [FMDatabaseQueue databaseQueueWithPath:file];
    [self.Queue inDatabase:^(FMDatabase *db) {
        BOOL judge = [db executeUpdate:@"create table if not exists t_Scroll(number INTEGER PRIMARY KEY AUTOINCREMENT,ScorllPic TEXT);"];
        if (judge) {
            NSLog(@"表创建成功");
        }
        else
        {
            NSLog(@"表创建失败");
        }
    }];
}

-(void)insert:(Discover *)dis
{
    [self.Queue inDatabase:^(FMDatabase *db) {

        BOOL judge = [db executeUpdate:@"insert into t_Scroll( ScorllPic ) values (?) ",dis.ScrollPic_url];
        
        if (judge) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
    }];
}

-(NSMutableArray *)GetScrollPic
{
    NSMutableArray *array = [NSMutableArray array];
    [self.Queue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from t_Scroll;"];
        while ([result next]) {
            NSString *pic = [result stringForColumn:@"ScorllPic"];
            [array addObject:pic];
        }
    }];
    return array;
}

-(void)deleteScrollpic
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_Scroll"];
        
    }];
    
    
}
//cell本地套装
-(void)CellInfoTable
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        BOOL judge = [db executeUpdate:@"create table if not exists t_Cell(number INTEGER PRIMARY KEY AUTOINCREMENT,CellName TEXT,CellDesc TEXT,CellPic TEXT);"];
        if (judge) {
            NSLog(@"表创建成功");
        }
        else
        {
            NSLog(@"表创建失败");
        }
    }];
}

-(void)insertCellInfo:(Discover *)dis
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        
        BOOL judge = [db executeUpdate:@"insert into t_Cell( CellName, CellDesc,CellPic) values (?,?,?) ",dis.CellName,dis.CellDesc,dis.CellPic_url];
        if (judge) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
    }];
}

-(NSMutableArray *)GetCellInfo
{
    NSMutableArray *array = [NSMutableArray array];
    [self.Queue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from t_Cell;"];
        while ([result next]) {
            NSString *pic = [result stringForColumn:@"CellPic"];
            NSString *name = [result stringForColumn:@"CellName"];
            NSString *desc = [result stringForColumn:@"CellDesc"];
            Discover *discover = [[Discover alloc]init];
            discover.CellPic_url = pic;
            discover.CellDesc = desc;
            discover.CellName = name;
            [array addObject:discover];
        }
    }];
    return array;
}

-(void)deleteCellInfo
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_Cell"];
    }];
}

//cellbutton本地套装
-(void)CellButtonInfoTable
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        BOOL judge = [db executeUpdate:@"create table if not exists t_CellButton(number INTEGER PRIMARY KEY AUTOINCREMENT,CollName TEXT,CollId TEXT,CollPic TEXT);"];
        if (judge) {
            NSLog(@"表创建成功");
        }
        else
        {
            NSLog(@"表创建失败");
        }
    }];
}
-(void)insertCellButton:(Discover *)dis
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        
        BOOL judge = [db executeUpdate:@"insert into t_CellButton( CollName, CollId,CollPic) values (?,?,?) ",dis.CollectionName,dis.CollectionId,dis.CollectionPic_url];
        if (judge) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
    }];
}

-(NSMutableArray *)GetCellButton
{
    NSMutableArray *array = [NSMutableArray array];
    [self.Queue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from t_CellButton;"];
        while ([result next]) {
            NSString *Name = [result stringForColumn:@"CollName"];
            NSString *ID = [result stringForColumn:@"CollId"];
            NSString *pic = [result stringForColumn:@"CollPic"];
            Discover *discover = [[Discover alloc]init];
            discover.CollectionPic_url = pic;
            discover.CollectionName = Name;
            discover.CollectionId = ID;
            [array addObject:discover];
        }
    }];
    return array;
}

-(void)deleteCellButton
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_CellButton"];
    }];
}

//cellbutton2本地套装
-(void)CellButton2InfoTable
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        BOOL judge = [db executeUpdate:@"create table if not exists t_CellButton2(number INTEGER PRIMARY KEY AUTOINCREMENT,CollName TEXT,CollId TEXT,CollPic TEXT);"];
        if (judge) {
            NSLog(@"表创建成功");
        }
        else
        {
            NSLog(@"表创建失败");
        }
    }];
}

-(void)insertCellButton2:(Discover *)dis
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        
        BOOL judge = [db executeUpdate:@"insert into t_CellButton2( CollName, CollId,CollPic) values (?,?,?) ",dis.CollectionName,dis.CollectionId,dis.CollectionPic_url];
        if (judge) {
            NSLog(@"插入成功");
        }
        else
        {
            NSLog(@"插入失败");
        }
    }];
}

-(NSMutableArray *)GetCellButton2
{
    NSMutableArray *array = [NSMutableArray array];
    [self.Queue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from t_CellButton2;"];
        while ([result next]) {
            NSString *Name = [result stringForColumn:@"CollName"];
            NSString *ID = [result stringForColumn:@"CollId"];
            NSString *pic = [result stringForColumn:@"CollPic"];
            Discover *discover = [[Discover alloc]init];
            discover.CollectionPic_url = pic;
            discover.CollectionName = Name;
            discover.CollectionId = ID;
            [array addObject:discover];
        }
    }];
    return array;
}

-(void)deleteCellButton2
{
    [self.Queue inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"delete from t_CellButton2"];
    }];
}



@end
